package cn.com.pajx.pajx_visitor_v3.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.pajx.pajx_visitor_v3.BaseApp;
import cn.com.pajx.pajx_visitor_v3.bean.UserRoleBean;

/**
 * create by ruancw on 2024/2/28.
 * sharePreferences数据保存类
 */

public class SharePreferencesUtil {

    private static SharedPreferences sharedPreferences;
    private static SharePreferencesUtil instance = null;

    public static SharePreferencesUtil getInstance() {
        if (instance == null) {
            instance = new SharePreferencesUtil();
            sharedPreferences = BaseApp.getInstance().getSharedPreferences("PAJX_SPP", Context.MODE_PRIVATE);
        }
        return instance;
    }

    /**
     * 保存登录后返回的loginBean
     *
     * @param userBean 返回的数据类
     * @return boolean
     */
    public boolean saveUserBean(UserRoleBean userBean) {
        Gson gson = new Gson();
        String userStr = gson.toJson(userBean);
        return putString("UserBean", userStr);
    }

    /**
     * 获取保存的登录成功后的返回数据
     *
     * @return UserBean
     */
    public UserRoleBean getUserRoleBean() {
        String message = getString("UserBean");
        if (message == null) {
            return null;
        }
        Gson gson = new Gson();
        return gson.fromJson(message, UserRoleBean.class);
    }

    /**
     * 保存string类型数据
     *
     * @param key   保存数据的key
     * @param value 保存的数据
     * @return boolean
     */
    public boolean putString(String key, String value) {
        return sharedPreferences.edit().putString(key, value).commit();
    }

    /**
     * 获取保存的String型数据
     *
     * @param key 保存数据时的key
     * @return String型数据
     */
    public String getString(String key) {
        return sharedPreferences.getString(key, null);
    }


    /**
     * 保存int型数据
     *
     * @param key   key值
     * @param value 数据
     * @return 布尔型
     */
    public boolean putInt(String key, int value) {
        return sharedPreferences.edit().putInt(key, value).commit();
    }

    /**
     * 获取int型数据
     *
     * @param key key
     * @return int
     */
    public int getInt(String key) {
        return sharedPreferences.getInt(key, -1);
    }


    /**
     * 保存long型数据
     *
     * @param key   key值
     * @param value 保存的数据
     * @return 布尔型
     */
    public boolean putLong(String key, long value) {
        return sharedPreferences.edit().putLong(key, value).commit();
    }

    /**
     * 获取long型数据
     *
     * @param key key值
     * @return long
     */
    public long getLong(String key) {
        return sharedPreferences.getLong(key, -1);
    }

    /**
     * 保存float类型数据
     *
     * @param key   key
     * @param value value
     * @return 布尔型
     */
    public boolean putFloat(String key, float value) {
        return sharedPreferences.edit().putFloat(key, value).commit();
    }

    /**
     * 获取float型数据
     *
     * @param key key
     * @return float
     */
    public float getFloat(String key) {
        return sharedPreferences.getFloat(key, -1);
    }

    /**
     * 保存boolean数据
     *
     * @param key   key
     * @param value value
     * @return boolean
     */
    public boolean putBoolean(String key, boolean value) {
        return sharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 获取boolean值
     *
     * @param key key
     * @return boolean
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * 保存List
     *
     * @param key      key
     * @param dataList list
     */
    public boolean putList(String key, List<String> dataList) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("int", dataList.size());
        for (int i = 0; i < dataList.size(); i++) {
            editor.putString(key + i, dataList.get(i));
        }
        return editor.commit();

    }

    /**
     * 获取List
     *
     * @param key key
     * @return list
     */
    public List<String> getList(String key) {
        List<String> environmentList = new ArrayList<>();
        int environNums = sharedPreferences.getInt("int", 0);
        for (int i = 0; i < environNums; i++) {
            String environItem = sharedPreferences.getString(key + i, null);
            environmentList.add(environItem);
        }
        return environmentList;
    }

    public void setMap(String key, LinkedHashMap<String, String> data) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        JSONArray mJsonArray = new JSONArray();
        Iterator<Map.Entry<String, String>> iterator = data.entrySet().iterator();
        JSONObject object = new JSONObject();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            try {
                object.put(entry.getKey(), entry.getValue());
            } catch (JSONException ignored) {
            }
        }
        mJsonArray.put(object);
        editor.putString(key, mJsonArray.toString());
        editor.apply();
    }

    public LinkedHashMap<String, String> getMap(String key) {
        LinkedHashMap<String, String> data = new LinkedHashMap<>();
        String result = sharedPreferences.getString(key, "");
        try {
            JSONArray array = new JSONArray(result);
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObject = array.getJSONObject(i);
                JSONArray names = itemObject.names();
                if (names != null) {
                    for (int j = 0; j < names.length(); j++) {
                        String name = names.getString(j);
                        String value = itemObject.getString(name);
                        data.put(name, value);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return data;
    }

    public void clearAll() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }

}
